<template>
	<view>
		<view>
			<navbar  title="商品管理" :isGoBack="true" :isNotice="true" ></navbar>
		</view>			
		<view class="goods_add">
		  <navigator class="cash_submit" url="../goodsadd/index?cashType=storage"  open-type="navigate">添加商品</navigator>  
		</view>
		<view class='productNav'> 
		     <!-- 左侧 -->
		    <view class='left'>
				  <scroll-view scroll-y scroll-with-animation style="height:100%;background: #f2f2f2;">
				   <view v-for="(item,index) in goodsCate" :key="index" :class="(active==index?'selected':'normal')"   @click='switchNav(item.id,index)'>{{item.name}}</view>
				  </scroll-view>
		    </view>
			 <!-- 右侧 -->
			 <view class='right'>			
			    <view style="width: 100%;border-bottom: 1px solid #f3f3f3;display: flex;flex-direction: row; ">
					<scroll-view class="myScroll"  scroll-x="true">
					  <!-- 快捷布局 .row{$}*8 $ 往里面代入值-->
					  <view class="row" :class="(pid==item.id?'active':'')" v-for="(item,index) in cate_list_level" :key="index" @click="onChange(item.id)" >{{item.name}}</view>
					</scroll-view>
			    </view>			
			    <view class='type'>
				<swiper class="tab-content"  duration="300"  :style="{'height':winHeight+'px'}">
					<!-- 全部 -->
					<swiper-item>
						<scroll-view :scroll-y="true" class="new-list-box" @scrolltolower="allLoadMore" scroll-top="scrollTop" scroll-with-animation :refresher-enabled="true" refresher-threshold="80" refresher-default-style="white" refresher-background="white" :refresher-triggered="triggered" @refresherrefresh="onAllRefresh">
							<view class="containerOne">
							    <block v-if="goodsList.length > 0">
								    <view class="item" v-for="(goods,index) in goodsList"  :key="index">
										<view class="flex">
											<image class="grid-img" :src="goods.goods_logo" @click.stop="goDetails(goods.id)" mode="scaleToFill"></image>
											<view class="cate-zt-main">
											  <view class="cate-zt zt_cate productNameText" style="font-size: 33rpx;"   @click.stop="goDetails(goods.id)">{{goods.goods_title}}</view> 
											  <view class="cate-zt zt_cate "  @click.stop="goDetails(goods.id)">
												<view class="pr-15 bg-white">销量 {{goods.package_sale}}</view>
												<view class="bg-white">库存 {{goods.goods_stock}}</view>
											  </view> 
											  <view class="cate-zt zt_cate justify-between">
												   <view class="pr-30 text-lg">¥{{goods.goods_price}}</view>
												   <text class="custom-btn-text" style="margin-left: 291rpx;margin-top: -31rpx;z-index: 4;border: 1px solid;" v-if="goods.car_num>0 && shopInfo.status == 1">{{goods.car_num}}</text>
												   <button class="boton-submit-check"   v-if="goods.checkStatus==0">待审核</button>
													<button class="boton-submit"  v-if="goods.checkStatus==1">销售中</button>
													<button class="boton-submit-fail" v-if="goods.checkStatus==2">不通过</button>
											  </view>															 
										</view>					 
								   </view> 
									
									<view class="specs-top-title" style="display: flex; align-items: center;">
										 <text class="mb-20 mr-15">状态:</text>
										  <view class="switch"> 
										  <u-switch v-model="goods.status == 0 ? false : true"  asyncChange @change="asyncChange(goods)"  ></u-switch>
										  </view>
										 <text style="margin-top:-20rpx;" v-if="goods.status==1">上架</text>
										 <text style="margin-top:-20rpx;" v-if="goods.status==0">下架</text>
									</view>
									
									<view class="flex justify-around" style="border-bottom: 1px solid #f3f3f3; padding-bottom: 10rpx;">
										<u-button type="primary" size="small" customStyle="margin-right:15rpx;margin-left:20rpx;width:160rpx" @click="editGoods(goods.id)" >编辑</u-button>
										<u-button type="warning" size="small" customStyle="margin-right:15rpx;width:160rpx" @click="editGoodsStock(goods.id)" >修改库存</u-button>
										<u-button  type="error" size="small"  customStyle="margin-right:20rpx;width:160rpx" @click="delGoods(goods.id)">删除</u-button>
																	
								    </view>
								 </view>								
							    </block>
							</view> 							                
						</scroll-view>
				    </swiper-item>	
				</swiper> 
				</view>
			</view>
		</view>	
	</view>
	
</template>


<script>
	import submitbar from "@/components/submit-bar/index.vue";
	import specsshop from "@/components/specs-shop/index.vue";
	import goodsShop from "@/components/pay_list/index.vue";

	export default {
		components: {
		   "t-submit-bar": submitbar,
		   "biz-specs":specsshop,
		   "biz-pay":goodsShop
		 },
		data() {
			return {
				showSelection:true,
				specSelection:false,
				goodsSelection:false,
				active:0,
				winHeight:'',
				pageNo:1,
				allTabList:[], // 全部  
				currentTab: 1, // 当前所在滑块的 index
				scrollTop: 0, // 设置竖向滚动条位置
				triggered: false, // 下拉是否激活
				cateId:"",
				pid:0,
				goodsCate:[],
				goodsList:[],
				shopInfo:{},
				car_total:0,
				setting_money:0.00,
				cate_list_level:[],
			}
		},
		onLoad() {
			let height = uni.getSystemInfoSync().windowHeight;
			this.winHeight = (height - 150);	
			
		},
		onShow() {
			this.goodsList=[]
			this._requestPageList()
		},
		methods:{
			// 参数
			_requestPageList(isLoadMore = false, callback) {
			  if(!isLoadMore){
				this.allTabList=[]; // 全部       
				this.pageNo= 1;				
			  }
			  let opt = {								
				token:uni.getStorageSync('userInfo').app_openid,
				cateId:this.cateId,
				pid:this.pid,
				pageNo :this.pageNo,
				login_type:'app',				
			  }    
			  this._requestPageListCom("virtualTaskList", opt, callback);
			},
			// 列表接口
			_requestPageListCom(url, params, callback) { 
			  let that=this
			  this.$http.goodsCateList(params).then(res => {
				if (res.resCode == "9999") {
					that.pid   = res.data.pid,
					that.goodsCate = res.data.cate_list,				
					that.shopInfo  = res.data.shop_info,
					that.car_total = res.data.car_total,
					that.setting_money = res.data.car_money_total,
					that.cate_list_level = res.data.cate_list_level					
					let oldList = [];
					oldList = this.goodsList;       
					const newGoodsList = res.data.goods_list
					const allData = [...oldList, ...newGoodsList]    
					this.goodsList=allData
					if(callback && typeof callback === 'function'){
						callback()
					}				 
				}
			  })
			},			  
			// 上拉加载  滚动到底部/右边时触发
			allLoadMore(e){
				let _self = this;
				if (this._allLoadMore) return
				this._allLoadMore = true;
				this.pageNo= ++_self.pageNo				
				this._requestPageList(true, () => {
				  _self._allLoadMore = false;
				});
			},
			
			// 下拉刷新接口
			onAllRefresh() {
			 // 开启下拉自定义样式
				this.triggered = true;
				this.pageNo=1;
				this.goodsList=[];
				this._requestPageList(false, () => {
					// 当处理完数据刷新后，wx.stopPullDownRefresh可以停止当前页面的下拉刷新
					this.triggered = false;
				});    
			},
			
			//菜单切换
			switchNav: function (id,active) {
				this.active=active
				this.cateId= id			  
				this.pageNo=1
				this.pid=0
				this.goodsList=[]
				this._requestPageList(true);			  
			},
			
			//横向切换
			onChange(id){			
			   this.pid=id,
			   this.goodsList=[]	
			   this._requestPageList(true);
			},			
			editGoods(goodsId){
				uni.navigateTo({
					url:"/pages/pagesuser/goodsadd/index?goodsId="+goodsId+"&cashType=edit"
				})
			},
			
			//修改库存
			editGoodsStock(goodsId){
				uni.navigateTo({
					url:"/pages/pagesuser/goodsStock/index?goodsId="+goodsId+"&cashType=edit"
				})
				
			},
		
		    //上下架
			asyncChange(event){
				let that=this
				let goodsId=event.id;
				let status=event.status==true?0:1;	
				let goodsList=this.goodsList
				this.$http.doeditStatus({goodsId,status}).then(res=>{
				    if(res.resCode=='9999'){
						for(let i=0;i<goodsList.length;i++){
							if(goodsList[i].id==goodsId) goodsList[i].status=status
						}						
						that.goodsList=goodsList
					}
				})			
			},			
			//删除商品
			delGoods(goodsId){	
				let that=this
				let goodsList=this.goodsList				
			    uni.showModal({content: '确定删除商品吗？'}).then(res=>{			
			      if(res.confirm){
			        this.$http.orderDel({goodsId}).then(res=>{
						if(res.resCode=='9999'){
						  uni.showToast({title:res.resMessage,icon:'none'})
						  setTimeout(()=>{
							   for(let i=0;i<goodsList.length;i++){
								if(goodsList[i].id==goodsId) goodsList.splice(i,1)
							   }						
							   that.goodsList=goodsList
						  },2000)          
						  return
						}else{
						  uni.showToast(res.resMessage)
						}
			        })       
			      }
			    })
			  },
			
			
			  
			  
			  
			  
			
			
		}
	}
</script>

<style>
	@import "./index.css";
</style>